module.exports = {main,sameElements};
const Item = require('../main/datbase');

function sameElements(inputs){
    let result=[];
    let obj={};
    for(let item of inputs){
        let count;
        let ch = item.slice(0,10);
        if(item.length>10){
          count = item.slice(11);
            if(!obj[ch]){
                obj[ch]={};
                obj[ch].barcode = ch;
                obj[ch].count =  parseInt(count);
            }else{
                obj[ch].count +=parseInt(count);
            }
       }
       else if(!obj[item]){
           obj[item]={};
           obj[item].barcode=item;
           obj[item].count=1;
       }else{
           obj[item].count +=1;
       }
       }
      for(let item in obj){
          result.push(obj[item]);
      }
      return result;
    }

    function sameElementsPrice(inputPrice){
        let allItems=Item.loadAllItems();
       
        for(let item1 of allItems){
            for(let item2 of inputPrice){
                if(item1.barcode===item2.barcode){
                   item2.price=item1.price;
                   item2.name=item1.name;
                   item2.unit=item1.unit;
                }
            }
        }
        return inputPrice;
    }//加上单价

    function on_sale(input){
        let promotion =Item.loadPromotions()[0].barcodes;
       
         for(let item of input){
                if(promotion.indexOf(item.barcode)){
                   item.priCount = item.count-parseInt(item.count/3);
            }
         }
         return input;
    }
    //商品优惠
 function print(arrInput){
        let promotion = Item.loadPromotions()[0].barcodes;
        let arr = [];
        let pay = '';
        let pri = '';
        let pay_price;
        let sum_price=0;
        let pri_price=0;
        for(let item of arrInput){
            if(item.count === item.priCount){
                pay_price = item.count*item.price;
                pay+=`名称：${item.name}，数量：${item.count}${item.unit}，单价：${item.price.toFixed(2)}(元)，小计：${ pay_price.toFixed(2)}(元)\n`
            }else{
                pay_price = item.priCount*item.price;
                pay+=`名称：${item.name}，数量：${item.count}${item.unit}，单价：${item.price.toFixed(2)}(元)，小计：${pay_price.toFixed(2)}(元)\n`   
            }
            sum_price = sum_price + pay_price;
        }
       for(let item of arrInput){
           if(item.count != item.priCount){
            var count1,price1;
              count1=item.count-item.priCount;
              pri+=`名称：${item.name}，数量：${count1}${item.unit}\n`
              price1=count1*item.price;
              pri_price = pri_price + price1;
           }
       }
       arr.push(pay);
       arr.push(pri);
       arr.push(sum_price.toFixed(2)) ;
       arr.push(pri_price.toFixed(2));
       return arr ;
    }
    //输出

 function main(inputs) {
       let inputPrice = sameElements(inputs);
       let input = sameElementsPrice(inputPrice);
       let arrInput = on_sale(input);
       let arr = print(arrInput);
    let str = '***<没钱赚商店>购物清单***\n' +arr[0]+
    '----------------------\n' +
    '挥泪赠送商品：\n' +arr[1]+
    '----------------------\n' +
    `总计：${arr[2]}(元)\n` +
    `节省：${arr[3]}(元)\n` +
    '**********************';
    console.log(str) ;
};